<template>
  <!-- 主体区域 -->
  <section id="app">
    <!-- 输入框 -->
    <TodoHeader @addArr="fn2"></TodoHeader>
    <!-- 列表区域 -->
    <TodoMain :arr="arr" @delArr="fn1"></TodoMain>
    <!-- 统计和清空 -->
    <TodoFooter :arr="arr" @removeArr="fn3"></TodoFooter>
  </section>
</template>

<script>
import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodeMain.vue";
import TodoFooter from "./components/TodeFooter.vue";
// 导入
export default {
  // 数据
  data() {
    return {
      arr: JSON.parse(localStorage.getItem("arr")) || [
        { id: 1, todo: "打篮球" },
        { id: 2, todo: "羽毛球" },
      ],
    };
  },
  methods: {
    // 删除功能实现
    fn1(val) {
      this.arr = this.arr.filter((e) => val !== e.id);
    },
    // 添加功能实现
    fn2(val) {
      this.arr.unshift(val);
    },
    // 清空
    fn3(val) {
      this.arr = val;
    },
  },
  watch: {
    arr: {
      deep: true,
      handler(val) {
        localStorage.setItem("arr", JSON.stringify(val));
      },
    },
  },
  // 注册
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
};
</script>

<style>
</style>